﻿
Imports System.Threading

Public Class tally
    Private _step As Int32
    Private hi As Int32
    Private _count As Int32

    Public Property [step]() As Int32
        Get
            Return _step
        End Get
        Set(ByVal value As Int32)
            atomic.eva(_step, value)
        End Set
    End Property

    Public Function count() As UInt64
        If _count < 0 Then
            Return (hi.asint64() + 1) << 32 + _count
        Else
            Return hi.asint64() << 32 + _count
        End If
    End Function

    Public Function tick() As Boolean
        Dim v As Int32 = 0
        v = Interlocked.Increment(_count)
        If v = Int32.MinValue Then
            Interlocked.Increment(hi)
        End If
        Return _step <= 0 OrElse v Mod _step = 0
    End Function

    Public Sub New()
    End Sub

    Public Sub New(ByVal [step] As Int32)
        Me.New()
        Me.[step] = [step]
    End Sub
End Class
